<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Community extends CI_Controller {

	protected $ajax_increment;
	
	public function __construct($community)
	{		
		parent::__construct();
		
		$this->load->model('community_model');			
		$this->load->model('feeds');		
		$this->load->helper('text');
		$this->load->library("pagination");
		/*
			-------------------------Load Themes--------------------------
			Load themes and define theme directory
		*/
		
		$this->config->load('themes');
		$theme = $this->config->item($community);
		if($theme == "" || !isset($theme))
			$theme = $this->config->item('global_theme');
			
		$this->data['css_dir'] = base_url()."assets/themes/".$theme."/css/";
		$this->data['js_dir'] = base_url()."assets/themes/".$theme."/js/";		
		$this->data['img_dir'] = base_url()."assets/themes/".$theme."/images/";		
		$this->data['ads_dir'] = base_url()."assets/themes/".$theme."/images/ads/";		
		$this->theme = $theme;
		
		/*
			-----------------------END THEME--------------------------------
		*/
		
		//load data for communities
		$this->community = $this->community_model->fetch_community_data($community);
		$this->data['logo'] = $this->community->strPicture;
		$this->data['title'] = $this->community->strName;	
		$this->data['community'] = 	$this->community;	
		$this->data['featured_images'] = $this->community_model->fetch_featured_content($this->community->intID);				
		$this->data['logged_in'] = $this->session->userdata('user_logged');
        $this->data['user'] = $this->session->all_userdata();
		$this->data['ptmenu'] = $this->load->view('common/pt_menu',$this->data,true);		
		
						
	}
	
	//AJAX FUNCTIONS------------------------------------------------------------------------------
	public function get_content_info()
	{		
		$post = $this->input->post();			
		$this->data['content'] = $this->community_model->fetch_single_content($post['id']);		
		$this->data['type'] = $post['type'];
		
		switch(get_type_name($post['type']))
		{
			case 'gallery':
				$this->data['gallery_images'] = $this->data_fetcher->fetch_table('tb_sys_media',null,null,array('fk-tb_mas_content'=>$post['id']));
				$html = $this->load->view('single_content_gallery_ajax',$this->data,true);		
				break;
			default:
				$html = $this->load->view('single_content_ajax',$this->data,true);		
		}
		
		$data['view_html'] = $html;
		echo json_encode($data);
	}
	
	public function content($type,$id,$title=null)
	{				
		$this->data['content'] = $this->community_model->fetch_single_content($id);
		//$this->data['title'] = $this->data['content']['strTitle'];
		//$this->data['title']= simplify_title($this->data['content']['strTitle']);
		//$this->data['title']= simplify_title($this->$title['content']['strTitle']);
		$this->data['title']= simplify_title($title);
		$this->data['tags'] = $this->community_model->fetch_tags($this->data['content']['strTags'],$id);
		//$this->data['tags'] = $this->data['content']['strTags'];
		$this->load->view('header_content',$this->data);
        $this->data['more_content'] = $this->community_model->fetch_content_type_archive($type,$this->community->intID,10,0,$id);
		//$this->data['related_content'] = $this->community_model->fetch_tags($type,$,l.this->community->intID);
		//print_r($this->data['more_content']);
		switch($type)
		{
			case 'gallery':
                $this->data['gallery_images'] = $this->data_fetcher->fetch_table('tb_sys_media',null,null,array('fk-tb_mas_content'=>$id));
				$this->load->view('single_content_gallery',$this->data);		
				break;
			case 'show':
				$this->data['more_content'] = $this->community_model->fetch_content_type_shows($type,$this->community->intID,10,0,$id);
				$this->load->view('shows_modal',$this->data);		
				break;
			case 'event':
				
				$this->load->view('event_modal',$this->data);		
				break;	
			default:
				$this->load->view('single_content',$this->data);		
		}
			
		$this->load->view('single_footer',$this->data);
				
	}
	
	public function archive($typeName,$offset=0,$limit=5)
	{	
		$this->load->view('header_content',$this->data);	
        $this->data['offset'] = $offset;
        $this->data['limit'] = $limit;
        $this->data['typename'] = $typeName;
		$this->data['content'] = $this->community_model->fetch_content_type_archive($typeName,$this->community->intID,$limit,$offset);
        $this->data['more_content_news'] = $this->community_model->fetch_content_type_archive('news',$this->community->intID,5,0);
        $this->data['more_content_features'] = $this->community_model->fetch_content_type_archive('features',$this->community->intID,5,0);
        $this->data['more_content_reviews'] = $this->community_model->fetch_content_type_archive('reviews',$this->community->intID,5,0);
        $this->data['archive_count'] = count($this->community_model->fetch_content_type_archive($typeName,$this->community->intID,150,0));
        //print_r($this->data['content']);
		$this->load->view('archive_gallery', $this->data);
		$this->load->view('single_footer',$this->data);		
		//sample
		
	}
    
    public function tags($tagName,$offset=0,$limit=5)
	{	
		$this->load->view('header_content',$this->data);	
        $this->data['offset'] = $offset;
        $this->data['limit'] = $limit;
        $this->data['tag'] = urldecode($tagName);
		$this->data['content'] = $this->community_model->fetch_content_tags(urldecode($tagName),$this->community->intID,$limit,$offset);
        $this->data['more_content_news'] = $this->community_model->fetch_content_type_archive('news',$this->community->intID,5,0);
        $this->data['more_content_features'] = $this->community_model->fetch_content_type_archive('features',$this->community->intID,5,0);
        $this->data['more_content_reviews'] = $this->community_model->fetch_content_type_archive('reviews',$this->community->intID,5,0);
        $this->data['archive_count'] = count($this->community_model->fetch_content_tags(urldecode($tagName),$this->community->intID,150,0));
        //print_r($this->data['content']);
		$this->load->view('tags_modal', $this->data);
		$this->load->view('single_footer',$this->data);		
		//sample
		
	}
	
	//----------------------------AJAX FUNCTIONS-----------------------------------------------------------------------------------------//
	
    public function archive_ajax()
    {
        $post = $this->input->post();
        $content = $this->community_model->fetch_content_type_archive($post['type'],$this->community->intID,5,$post['offset']);
        echo json_encode($content);
    }
    public function get_ajax_feed()
	{
		$first_element = 1;				
		
		$cid = $this->input->post('cid');
		$direction = $this->input->post('direction');
		$current = $this->input->post('current');
		$type = get_type_id($this->input->post('type'));
		$data_feed = array();
		
		$count = $this->feeds->get_feed_count($cid,$type);				
		$diff = $count - $current;
		
		if($current == $first_element && $direction == 'left')
		{
			$offset = 1;			
		}
		elseif( $diff < $this->ajax_increment && $direction == 'right')
		{	
			if($count < $this->ajax_increment)
				$offset = 1;
			else
				$offset = $current+$diff;			
		}		
		elseif($direction == 'left')
			$offset = $current - $this->ajax_increment;
		else			
			$offset = $current + $this->ajax_increment;
					
		$feeds = $this->feeds->fetch_latest_feeds($cid,$this->ajax_increment,$offset-1,$type);
		
		$data['current'] = $offset;		
		foreach($feeds as $feed)
		{
			$feed['ct-strContent'] = word_limiter(strip_tags($feed['ct-strContent']),15);
			$timestamp = strtotime($feed['dteStart']);
			$feed['dteStart'] = date("m/d/y",$timestamp);
			$data_feed[] = $feed;
		}
		$this->data['feeds'] = $data_feed;
		$data['returned'] = count($data_feed);
		$data['count'] = $count;
		$data['html_view'] = $this->load->view('ajax_feed',$this->data,true);
		echo json_encode($data);
	}
}